import os
from dao.project_url import url
from myadmin.models import Pod, Cluster

url = url()


def get_pods():
    cluster_list = Cluster.objects.filter(status__gt=0)  # 获取当前集群列表
    init_pods()  # 扫描前先将数据库中所有pod置为状态0
    for cluster in cluster_list:
        os.system("kubectl get pod -n %s 2> /dev/null | grep -v NAME > %s/static/pod/pod_tmp.txt" % (cluster.name, url))
        # pod名字
        os.system("awk '{print $1}' %s/static/pod/pod_tmp.txt > %s/static/pod/name.txt" % (url, url))
        # pod状态
        os.system("awk '{print $3}' %s/static/pod/pod_tmp.txt > %s/static/pod/status.txt" % (url, url))
        # pod所属主机
        os.system("kubectl describe pod -n %s | grep ^Node: > %s/static/pod/ip_tmp.txt" % (cluster.name, url))
        os.system("awk -F '/' '{print $2}' %s/static/pod/ip_tmp.txt > %s/static/pod/ip.txt" % (url, url))
        # pod cpu和内存使用情况
        os.system("kubectl top pods -n %s | grep -v NAME > %s/static/pod/cpu_tmp.txt" % (cluster.name, url))
        os.system("awk '{print $2}' %s/static/pod/cpu_tmp.txt > %s/static/pod/cpu.txt" % (url, url))
        os.system("awk '{print $3}' %s/static/pod/cpu_tmp.txt > %s/static/pod/memory.txt" % (url, url))
        with open("{}/static/pod/name.txt".format(url), 'r') as n, open("{}/static/pod/status.txt".format(url),'r') as s, open("{}/static/pod/ip.txt".format(url), 'r') as i, open("{}/static/pod/cpu.txt".format(url), 'r') as c, open("{}/static/pod/memory.txt".format(url), 'r') as m:
            name_list = n.readlines()
            status_list = s.readlines()
            ip_list = i.readlines()
            cpu_list = c.readlines()
            memory_list = m.readlines()
        n.close()
        s.close()
        i.close()
        c.close()
        m.close()
        i = 0
        for name, status, ip in zip(name_list, status_list, ip_list):
            if Pod.objects.filter(name=name.rstrip()).exists():
                ob = Pod.objects.get(name=name.rstrip())
                ob.hosts_ip = ip.rstrip()
                ob.cluster_name = cluster.name
                if status.rstrip() == 'Running':
                    ob.status = 1
                    ob.cpu = cpu_list[i]
                    ob.ram = memory_list[i]
                    i += 1
                else:
                    ob.status = 2
                    ob.cpu = '0m'
                    ob.ram = '0Mi'

                ob.save()
            else:
                ob = Pod()
                ob.name = name.rstrip()
                ob.hosts_ip = ip.rstrip()
                ob.cluster_name = cluster.name
                if status.rstrip() == 'Running':
                    ob.status = 1
                    ob.cpu = cpu_list[i]
                    ob.ram = memory_list[i]
                    i += 1
                else:
                    ob.status = 2
                    ob.cpu = '0m'
                    ob.ram = '0Mi'
                ob.save()


def init_pods():
    qs = Pod.objects.filter(status__gt=0)
    for ob in qs:
        ob.status = 0
        ob.save()